/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import IBO.IIntervention;
import IUSECASE.IUC_Factory;
import java.io.*;
import java.net.*;

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrateur
 */
public class InterventionUpdateToEncours extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        PrintWriter page = response.getWriter();
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        doPost(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
            RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/index.jsp");
            IUC_Factory factory = null;
            factory = (IUC_Factory) Naming.lookup("rmi://localhost:1099/factory");
            GregorianCalendar heureDebut = new GregorianCalendar();
            heureDebut.setTime(new Date());
            factory.getGestionOnSite().traiter((IIntervention) request.getSession().getAttribute("intervention"), heureDebut);
            this.gotoPage("/TechnicienInfo.jsp",request,response);
        } catch (NotBoundException ex) {
            Logger.getLogger(InterventionUpdateToEncours.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(InterventionUpdateToEncours.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(InterventionUpdateToEncours.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
    
    private void gotoPage(String adress, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher =
	getServletContext().getRequestDispatcher(adress);
        dispatcher.forward(request,response);
    }
}
